package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.query.PageResult;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * Created by Administrator on 2020/5/6.
 */
@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;


    @Override
    public Employee login(String username, String password) {
        Employee employee = employeeMapper.login(username, password);
        if(employee == null){
            throw new RuntimeException("账号或密码错误");
        }
        return employee;
    }


    @Override
    public void delete(Long id) {
        //删除角色关系
        employeeMapper.deleteRelation(id);
        employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Employee employee, Long[] ids) {
        //对密码进行加密
        Md5Hash md5Hash = new Md5Hash(employee.getPassword(),employee.getName());
        employee.setPassword(md5Hash.toString());
        employeeMapper.insert(employee);
        //处理关联关系,存入角色和员工的关系,单条添加,每次执行一条sql语句
/*        if (ids != null && ids.length>0){
            for (Long rid:ids){
                employeeMapper.insertRelation(employee.getId(),rid);
            }
        }*/
        //批量添加,一次执行多条sql语句
        if (ids != null && ids.length>0) {
            employeeMapper.insertBatchRelation(employee.getId(),ids);
        }
    }

    @Override
    public Employee get(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> listAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public void update(Employee employee, Long[] ids) {
        employeeMapper.updateByPrimaryKey(employee);
        //删除原来的,放在删除的方法里面
        employeeMapper.deleteRelation(employee.getId());
        //批量添加,一次执行多条sql语句
        if (ids != null && ids.length>0) {
            employeeMapper.insertBatchRelation(employee.getId(),ids);
        }
    }

    @Override
    public PageInfo<Employee> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Employee> employees = employeeMapper.queryForList(qo);
        return new PageInfo(employees);
    }

    @Override
    public void updatePwd(String name, String password) {
        Md5Hash md5Hash = new Md5Hash(password,name);
        employeeMapper.updatePwd(name,md5Hash.toString());
    }

    @Override
    public void batchDelete(Long[] ids) {
        employeeMapper.batchDelete(ids);
    }

    @Override
    public Employee selectByName(String name) {
        return employeeMapper.selectByName(name);
    }

    @Override
    public Workbook exportXls() {
        //获取所有的员工数据
        List<Employee> employees = employeeMapper.selectAll();
        //创建一个excel文件对象
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建一页纸
        Sheet sheet = workbook.createSheet("员工通讯录");
        //创建标题行
        //创建一行(索引为0)
        Row row = sheet.createRow(0);
        //创建单元格(传入列,索引为0);在单元格写内容
        row.createCell(0).setCellValue("姓名");
        row.createCell(1).setCellValue("邮箱");
        row.createCell(2).setCellValue("年龄");

        for(int i = 0;i < employees.size();i++){
            //获取每一个员工
            Employee employee = employees.get(i);
            //创建一行(索引为0)
            row = sheet.createRow(i+1);
            //创建单元格(传入列,索引为0);在单元格写内容
            row.createCell(0).setCellValue(employee.getName());
            row.createCell(1).setCellValue(employee.getEmail());
            if(employee.getAge()!=null){
                row.createCell(2).setCellValue(employee.getAge());
            }
        }
        return workbook;
    }

    @Override
    public void importXls(MultipartFile file) throws IOException {
        //读excel文件
        Workbook workbook = new HSSFWorkbook(file.getInputStream());
        //读哪一页的数据
        Sheet sheet = workbook.getSheetAt(0);
        //获取最后一行的行数
        int lastRowNum = sheet.getLastRowNum();
        //标题行不读
        for (int i = 1; i<=lastRowNum;i++){
            //获取每一行
            Row row = sheet.getRow(i);
            //创建员工对象
            Employee employee = new Employee();
            String name = row.getCell(0).getStringCellValue();
            //判断单元格类型的
//            CellType cellType = row.getCell(0).getCellType();
//            if (cellType.equals(CellType.STRING)){
//
//            }
            if(!StringUtils.hasLength(name)){
                continue;
            }
            employee.setName(name);
            //读数值的
//            double numericCellValue = row.getCell(0).getNumericCellValue();
            String email = row.getCell(1).getStringCellValue();
            employee.setEmail(email);
            String age = row.getCell(2).getStringCellValue();
            if(StringUtils.hasLength(age)){
                employee.setAge(Integer.valueOf(age));
            }
            //插入数据库
            employee.setPassword("1");
            this.save(employee,null);
        }
    }

    @Override
    public List<Employee> selectByRoleSn(String... sns) {
        return employeeMapper.selectByRoleSn(sns);
    }
}
